/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.zk.common.ctrl;

import com.timesoft.zk.common.ResourceUtil;
import java.util.Locale;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.GenericAutowireComposer;

/**
 *
 * @author sorasaks
 */
public class IncludePageWsController extends GenericAutowireComposer<Component> {

    private static final Logger LOG = Logger.getLogger(IncludePageWsController.class);

    protected String pageName;
    protected Locale locale;
    protected String contextPath;
    protected String realPath;
    protected String amountFormat;
    protected String dateFormat;
    protected Boolean isResource;
    
    protected String customerCode;
    protected Boolean authentification;

    public IncludePageWsController() throws Exception {
        this.pageName = "";
        this.isResource = Boolean.FALSE;
        initialParameter();
        LOG.info("::::::::::: pageName :::::::::::::" + pageName);
        LOG.info("::::::::::: locale :::::::::::::" + locale);
        LOG.info("::::::::::: contextPath :::::::::::::" + contextPath);
        LOG.info("::::::::::: realPath :::::::::::::" + realPath);
        LOG.info("::::::::::: amountFormat :::::::::::::" + amountFormat);
        LOG.info("::::::::::: dateFormat :::::::::::::" + dateFormat);
    }

    public IncludePageWsController(String _pageName, Boolean _isResource) throws Exception {
        this.pageName = _pageName;
        this.isResource = _isResource;
        initialParameter();
        LOG.info("::::::::::: pageName :::::::::::::" + pageName);
        LOG.info("::::::::::: locale :::::::::::::" + locale);
        LOG.info("::::::::::: contextPath :::::::::::::" + contextPath);
        LOG.info("::::::::::: realPath :::::::::::::" + realPath);
        LOG.info("::::::::::: amountFormat :::::::::::::" + amountFormat);
    }

    @Override
    public void doBeforeComposeChildren(Component comp) throws Exception {
        super.doBeforeComposeChildren(comp); //To change body of generated methods, choose Tools | Templates.
        if (!StringUtils.isEmpty(pageName)) {
            if (isResource) {
                setTitle(ResourceUtil.getLabel(pageName, locale));
            } else {
                setTitle(pageName);
            }
        }
    }

    protected void setTitle(String _pageName) {
        getPage().setTitle(_pageName);
    }

    protected void initialParameter() throws Exception {
        locale = (Locale) Executions.getCurrent().getSession().getAttribute(Attributes.PREFERRED_LOCALE);
        realPath = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_REAL_PATH);
        contextPath = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_CONTEXT_PATH);
        dateFormat = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_DATE_FORMAT);
        amountFormat = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_AMOUNT_FORMAT);
        customerCode = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_CUSTOMER_CODE);
        authentification = (Boolean) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_AUTHENTIFICATION);
    }
}
